#include "mainwindow.h"

#include <QDebug>
#include <QDir>
#include <QPluginLoader>
#include <QStandardItemModel>
#include <QVBoxLayout>

#include "PluginInterface.h"
#include "core/core.h"
#include "core/pluginmanager.h"
#include "plugindialog.h"
#include "ui_mainwindow.h"

#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  connect(ui->action_plugin, &QAction::triggered, this,
          [=] { PluginDialog().exec(); });

  auto addNavigation = [=](const QString &name, QWidget *wt) {
    auto button = new QPushButton(name, this);
    auto layout = qobject_cast<QVBoxLayout *>(ui->frame_navigation->layout());
    layout->insertWidget(layout->count() - 1, button);

    auto index = ui->stackedWidget->addWidget(wt);
    connect(button, &QAbstractButton::clicked, this,
            [=] { ui->stackedWidget->setCurrentIndex(index); });
  };

  auto manager = core()->getPluginManager();
  for (auto const &i : manager->getPluginMetaData()) {
    if (!manager->isLoad(i.IID))
      continue;
    auto widget = manager->getPluginWidget(i.IID);
    auto name = i.name;
    addNavigation(name, widget);
  }
}

MainWindow::~MainWindow() { delete ui; }
